package com.xb.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.xb.entity.Meeting;
import com.xb.entity.PageResult;
import com.xb.entity.Result;
import com.xb.entity.User;
import com.xb.service.MeetingService;
import com.xb.service.UserService;
import com.xb.websocket.XBWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("meeting")
public class MeetingController {

    @Autowired
    MeetingService meetingService;

    @Autowired
    UserService userService;

    @Autowired
    HttpSession session;

    /**
     * 查询会议列表
     * @param searchMap 查询会议名 状态
     * @param page 当前页
     * @return 响应结果集
     */
    @RequestMapping("/search/{page}")
    public Result search(@RequestBody Map searchMap, @PathVariable Integer page){
        Page<Meeting> pageData = meetingService.findMeeting(searchMap, page);
        PageResult<Meeting> pageResult = new PageResult<>(pageData.getTotalPages(), pageData.getContent());
        Map returnMap = new HashMap();
        returnMap.put("pageResult", pageResult);
        return new Result(true, "查询成功", returnMap);
    }

    /**
     * 添加会议
     * @param meeting 会议信息
     * @return 响应结果信息
     */
    @RequestMapping("/addMeeting")
    public Result addMeeting(@RequestBody Meeting meeting){
        meetingService.save(meeting);
        String[] users = meeting.getMakeUser().split(",");

        /**
         * 给此次会议所要参加的用户推送信息
         */
        for (String userId : users) {
            String message = "您接收到一个会议,开始时间为" +
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(meeting.getStartTime()) +
                    "记得准时参加哦！";
            XBWebSocket.sendMessage(Long.parseLong(userId),message);
        }
        return new Result(true,"添加成功");
    }

    /**
     * 查看会议详情
     * @param meetingId 会议ID
     * @return 响应结果集
     */
    @RequestMapping("/detail/{meetingId}")
    public Result detail(@PathVariable Long meetingId){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        // 返回结果集
        Map<String, Object> map = new HashMap<>();
        // 会议详情
        Meeting meeting = meetingService.findById(meetingId);
        map.put("meetingInfo", meeting);
        // 登录用户是否需要参加会议
        Boolean flag = false;
        String[] makeUsers = meeting.getMakeUser().split(",");
        // 记录登录用户是否需要参加会议
        for (String userId : makeUsers) {
            if (userId.equals(loginUser.getId()+ "")) flag = true;
        }
        // 应到人数
        Integer makeUser = makeUsers.length;
        map.put("makeUser", makeUser);
        // 实到人数
        Integer meetingJoin = meetingService.countMeetingJoinByMeetingId(meeting.getId());
        map.put("meetingJoin", meetingJoin);
        // 未到人数
        map.put("noJoin", makeUser - meetingJoin);
        // 如果用户需要参加会议,查询用户是否参加了会议
        flag = (flag)?meetingService.loginUserIsJoinMeeting(meeting.getId(), loginUser.getId()):null;
        map.put("isJoin", flag);
        return new Result(true,"查询成功", map);
    }

    /**
     * 加入会议
     * @param mId 会议ID
     * @param state 会议状态 加入会议/退出会议
     * @return 响应结果集
     */
    @RequestMapping("/join")
    public Result join(Long mId, Boolean state){
        String username = (String) session.getAttribute("loginUser");
        User loginUser = userService.findByUsername(username);
        return new Result(true, meetingService.joinMeeting(loginUser.getId(), mId, state));
    }
}
